# clean paper 3 replication

library(ggplot2)
library(tidyverse)
library(plm)
library(marginaleffects)
library(sjPlot)
library(readxl)
library(haven)
library(gridExtra)
library(grid)

data(efc)
theme_set(theme_sjplot())

# load the date file 
why_accommodate <- read_dta("C:/...")

why_data1 <- why_accommodate
why_data2 <- subset(why_accommodate, mainstream2 == 1 & eastwest == 1 & year >= 1999)
why_data3 <- pdata.frame(why_data2, index = c("party_id", "year"))


# standardizing for plots
why_data3_standardized <- 
  why_data3 %>% 
  mutate(eurosceptic_niche_narrow_vote2_s = scale(eurosceptic_niche_narrow_vote2)) %>% 
  mutate(mean_eu_sal_scep_niche_nar_lag_s = scale(mean_eu_sal_scep_niche_nar_lag))


m1_s <- plm(eu_position_change ~ eurosceptic_niche_narrow_vote2_s + mean_eu_sal_scep_niche_nar_lag_s
            + eu_dissent_lag + government + vote + eu_distance2_lag 
            + eu_dissent_lag*eurosceptic_niche_narrow_vote2_s, data = why_data3_standardized, model = "within")

m2_s <- plm(eu_position_change ~ eurosceptic_niche_narrow_vote2_s + mean_eu_sal_scep_niche_nar_lag_s
            + eu_dissent_lag + government + vote + eu_distance2_lag 
            + eu_dissent_lag*mean_eu_sal_scep_niche_nar_lag_s, data = why_data3_standardized, model = "within")

m3_s <- plm(eu_salience_change ~ eurosceptic_niche_narrow_vote2_s + mean_eu_sal_scep_niche_nar_lag_s
            + eu_dissent_lag + government + vote + eu_distance2_lag 
            + eu_dissent_lag*eurosceptic_niche_narrow_vote2_s, data = why_data3_standardized, model = "within")

m4_s <- plm(eu_salience_change ~ eurosceptic_niche_narrow_vote2_s + mean_eu_sal_scep_niche_nar_lag_s
            + eu_dissent_lag + government + vote + eu_distance2_lag 
            + eu_dissent_lag*mean_eu_sal_scep_niche_nar_lag_s, data = why_data3_standardized, model = "within")




plot1_s <- plot_slopes(m1_s,
                     conf_level = 0.95,
                     vcov = "HC1",
                     rug = TRUE,
                     variables = "eurosceptic_niche_narrow_vote2_s", condition = "eu_dissent_lag") +
  xlab("EU intra-party divisions (t-1)") +
  ylab("Effect of Eurosceptic niche party vote share on Δ EU position") +
  # Change the background
  theme_bw() +
  # Add the title
  ggtitle("") +
  theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed") +
  ylim(-0.5, 0.5)

plot2_s <- plot_slopes(m2_s,
                     conf_level = 0.95,
                     vcov = "HC1",
                     rug = TRUE,
                     variables = "mean_eu_sal_scep_niche_nar_lag_s", condition = "eu_dissent_lag") +
  xlab("EU intra-party divisions (t-1)") +
  ylab("Effect of Eurosceptic niche EU salience (t-1) on Δ EU position") +
  # Change the background
  theme_bw() +
  # Add the title
  ggtitle("") +
  theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed") +
  ylim(-0.5, 0.5)

grid.arrange(plot1_s, plot2_s, ncol = 2)


plot3_s <- plot_slopes(m3_s,
                     conf_level = 0.95,
                     vcov = "HC1",
                     rug = TRUE,
                     variables = "eurosceptic_niche_narrow_vote2_s", condition = "eu_dissent_lag") +
  xlab("EU intra-party divisions (t-1)") +
  ylab("Effect of Eurosceptic niche party vote share on Δ EU salience") +
  # Change the background
  theme_bw() +
  # Add the title
  ggtitle("") +
  theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed") +
  ylim(-1.5, 2)

plot4_s <- plot_slopes(m4_s,
                     conf_level = 0.95,
                     vcov = "HC1",
                     rug = TRUE,
                     variables = "mean_eu_sal_scep_niche_nar_lag_s", condition = "eu_dissent_lag") +
  xlab("EU intra-party divisions (t-1)") +
  ylab("Effect of Eurosceptic niche EU salience (t-1) on Δ EU salience") +
  # Change the background
  theme_bw() +
  # Add the title
  ggtitle("") +
  theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")+
  ylim(-1.5, 2)

grid.arrange(plot3_s, plot4_s, ncol = 2)

grid.arrange(plot1_s, plot2_s, plot3_s, plot4_s, ncol = 2)

